import {SAVE_USER_AND_TOKEN,LOGIN_OUT_USER_AND_TOKEN} from "../constant";
let user =JSON.parse(localStorage.getItem('user'));
let token = localStorage.getItem('token');
const preInfo = {
	token:token || '',
	user:user || {},
	isLogin: user && token ? true : false
}

export default function login(prevState=preInfo, action) {
	 
    const {type, data} = action;
    switch (type) {
        case SAVE_USER_AND_TOKEN:
        return {
            user: data.user,
            token: data.token,
            isLogin: true
        }
		case LOGIN_OUT_USER_AND_TOKEN:
		return {
		    user:'',
		    token:'',
		    isLogin: false
		}
        default:
            return prevState;
    }
}